﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace IHISCETeim.Library
{
    /// <summary>
    /// 对应数据库表字段名称
    /// </summary>
    public class QueryColumnNameAttribute : Attribute
    {
        public string ColumnName { get; set; }
        /// <summary>
        /// 手工输入字段名称
        /// </summary>
        /// <param name="columnName"></param>
        public QueryColumnNameAttribute(string columnName)
        {
            ColumnName = columnName;
        }
        /// <summary>
        /// 
        /// </summary>
        public QueryColumnNameAttribute()
        {
            ColumnName = "";
        }
    }
    /// <summary>
    /// 查询条件的的查询符，=，或 like ,
    /// </summary>
    public class QueryOperatorAttribute : Attribute
    {
        public QueryOperatorsEnum OperatorName { get; set; }
        public QueryOperatorAttribute(QueryOperatorsEnum operatorName)
        {
            OperatorName = operatorName;
        }
        public QueryOperatorAttribute()
        {
            OperatorName = QueryOperatorsEnum.等于;
        }
    }

    

    public static class QueryFiledHelper
    {
        public static string GetSqlWhere(object entryObj)
        {
            string sqlwhereReturn = "";
            //T entryObj = TObj as T;
            Type typeUnsu = entryObj.GetType();

            PropertyInfo[] propArry = typeUnsu.GetProperties();
          
            object provalue = null;
            string PropertyTypeString = "System.String";
            string QueryColumnName = "";
            QueryOperatorsEnum QueryOperator = QueryOperatorsEnum.等于;
            foreach (var item in propArry)
            {

                try
                {
                    provalue = item.GetValue(entryObj, null);//属性值
                    PropertyTypeString = item.PropertyType.ToString();
                    object[] objQueryColumnName = item.GetCustomAttributes(typeof(QueryColumnNameAttribute), false);
                    if (objQueryColumnName.Length > 0)//说明该属性使用了ExcelRowIndexAttribute标记
                    {
                        QueryColumnName = (objQueryColumnName[0] as QueryColumnNameAttribute).ColumnName;
                        QueryColumnName = QueryColumnName == "" ? item.Name : QueryColumnName;
                    }
                    else
                    {
                        continue;//没有行特性,进入下一个
                    }

                    object[] objQueryOperator = item.GetCustomAttributes(typeof(QueryOperatorAttribute), false);
                    if (objQueryOperator.Length > 0)//说明该属性使用了ExcelRowIndexAttribute标记
                    {
                        QueryOperator = (objQueryOperator[0] as QueryOperatorAttribute).OperatorName;
                    }
                    else
                    {
                        QueryOperator = QueryOperatorsEnum.等于;
                    }

                    // sqlwhereReturn += getSingFiledSql(QueryColumnName, PropertyTypeString, provalue, QueryOperator, sqlwhereReturn);
                    if (string.IsNullOrEmpty(provalue.ToString()))
                        continue;
                    sqlwhereReturn += (string.IsNullOrEmpty(sqlwhereReturn) ? " " : " and ") + QueryOperatorsEnumHelper.ConvertCommonOperator(QueryColumnName, QueryOperator, provalue.ToString());
                }
                catch {
                    continue;
                }
            }

            return sqlwhereReturn;
        }       
    }
}
